if (window.plus) {
	ready();
} else {
	document.addEventListener('plusready', ready, false);
}

$(function() {
	if (AGENT_CLIENT.WEB) {
		ready();
	}
})

function ready() {
	var
		_user, //用户信息
		_query = {
			"index": 1,
			"size": CONFIG_MANAGER.pageSize,
			"query": "",
			"search": "",
			"sorting": "",
		}, //请求条件
		html = [], //html字符串
		_myscroll = null, //滚动
		_thispage = null, //当前页
		_pullUp = null,
		_pullDown = null;

	var showPage = function() {
		this.init();
	};
	showPage.prototype = {
		init: function() {
			this.loadData();
			this.addScroll();		
			this.bindEvent();
		},
		addScroll: function() {
			_pullUp = $("#pullUp");
			_pullDown = $("#pullDown");
			_pullDown.hide();
			_myscroll = new IScroll('#wrapper', {
				probeType: 2,
				scrollbars: true,
				mouseWheel: false,
				fadeScrollbars: true,
				bounce: true,
				interactiveScrollbars: false,
				shrinkScrollbars: 'scale',
				click: true,
				keyBindings: true,
				momentum: true
			});
			_myscroll.on("scrollEnd", function() {
				console.log("this.y=" + this.y);
				console.log("this.maxScrollY=" + this.maxScrollY);
				if ((this.y < 0) && (this.y == this.maxScrollY)) {
					console.log("滚动到底部");
					_pullUp.show();
					setTimeout(function() {
						_query.index++;
						_thispage.loadShowList("add");
					}, 500);
				} else if ((this.y == 0) && (this.y != this.maxScrollY)) {
					console.log("滚动到顶部");
					_pullDown.show();
					setTimeout(function() {
						_query.index = 0;
						_thispage.loadShowList("renovate");
					}, 500);
				}
			});
		},
		//加载
		loadData: function() {
			_user = getLocalStorage(STORAGE_MANAGER.user);
			if (!_user) {
				redirect("../account/login.html");
				return false;
			}
			this.loadShowList();
		},
		loadShowList: function(action) {
			var requestUrl = CONFIG_MANAGER.RequstUrl + "v1/fans/my/";
			$.ajax({
				type: "POST",
				url: requestUrl,
				async: true,
				data: _query,
				headers: {
					"token": _user.token
				},
			}).done(function(data) {
				_thispage.loadList(JSON.parse(data), action);
			}).always(function(data) {});
		},
		//加载列表
		loadList: function(data, action) {
			var length = data.data.length,
				list = data.data,
				html = [];
			if (undefined == action && 0 == length) {
				$("#showList").html('<div class="zhanwushuju"><img src="../../images/zanwushuju.png"/><br>暂无数据</div>');
			} else {
				html = [];
				$.each(list, function(i, show) {					
					html.push('<div class="mbtopbottom sqshbg">');
					html.push('<div class="with100 overflow">');
					html.push('<div class="ddnybbt mbbottom">');
					html.push('<p class="dfktbdw">'+ show.audit_status_text +'</p>');
					if (1 == show.audit_status) {
						var photo = (show.albums && show.albums.length > 0) ? show.albums[0].photo : "";
						var tips = { id:show.id,photo:photo, memo:show.memo };
						html.push('<p class="dddpt"><span name="share" tips='+ JSON.stringify(tips) +' class="ljfx"></span></p>');
					}else if(2 == show.audit_status){
						html.push('<p class="dddpt"><span name="checkReason" tip="' + show.audit_memo + '" class="ljfx">查看原因</span></p>');
					}else{
						html.push('<p class="dddpt"><span class="ljfx"></span></p>');
					}
					html.push('</div>');
					html.push('<dl name="showItem" tip="' + show.id + '" class="cpxqxztop cpxqxztop1 mtop0">');
					var src = "../../images/place/181x220.png"
					if (show.albums && show.albums.length > 0) {
						src = getImageUrl(show.albums[0].photo, 62, 62);
					}
					html.push('<dt><img src="' + src + '"></dt>');
					html.push('<dd>');
					html.push(show.memo);
					html.push('</dd>');
					html.push('<dd class="wlxxlc wlxxlc5 huisezi"><span class="huisezi  floatr"><b>' + toDateString(show.create_time, 'yyyy-MM-dd hh:mm:ss') + '</b></span> 来源：小苹果</dd>');
					html.push('</dl>');
					html.push('</div>');
					html.push('</div>');
				});
				if ("add" == action) {
					$(html.join('')).appendTo($("#showList"));
				} else {
					$("#showList").html(html.join(''));
				}
			}
//			setTimeout(function() {
//				_myscroll.refresh();
//				if (0 == length) {
//					_pullUp.hide();
//				}
//				_pullDown.hide();
//			}, 100);
        setTimeout(function () {
            _myscroll.refresh();
            _pullUp.hide();
            _pullDown.hide();
        }, 100);
		},
		//页面事件
		bindEvent: function() {
			//点击返回
			$("#back").on("click", function() {
				if (AGENT_CLIENT.APP) {
					plus.webview.getWebviewById("../mine/mylife.html").evalJS("reloadPage()");
					plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(4)");
				} else {
					redirect("mylife.html");
				}
			});

			//立即分享
			$("#main").on("tap", "span[name='share']", function() {
				var data = $(this).attr('tip').split('|');
				_sharePage.shareInfo = {
					title: "我的星粉秀",
					content: data[2],
					href: CONFIG_MANAGER.shareUrl + "coin/coindetail.html?id=" + data[0],
					pic: getImageUrl(data[1], 200, 200),
					picid: data[1]
				};
				_sharePage.show();
			});

			//查看原因
			$("#main").on("click", "span[name='checkReason']", function() {
				var meno = $(this).attr('tip');
				$("#menoContent").html(meno);
				$("#popMeno").show();
			});

			//关闭弹窗
			$("#closeMeno").on("tap", function() {
				$("#popMeno").hide();
			});

			//添加星粉秀
			$("#gotoPublish").on("click", function() {
				redirect("showpublish.html");
			});

			//跳转到详情
			$("#main").on("click", "dl[name='showItem']", function() {
				var id = $(this).attr('tip');
				redirect("../coin/coindetail.html?id=" + id);
			});
		},


	};
	_thispage = new showPage();
	// 分享页面
	var _sharePage = new sharePage();
}